import { defineStore } from 'pinia';
import {getUserInfo, loginByPhone} from "@/api";

const useUserStore = defineStore('counter', {
	state: () => ({
		userInfo: {
            ancestorRelation: null,
            avatar: null,
            channel: null,
            firstLeader: null,
            gender: null,
            id: null,
            inviterCode: null,
            inviterId: null,
            isClose: 0,
            isNew: null,
            nickname: null,
            phone: null,
            realName: null,
            secondLeader: null,
            sn: null,
            status: 1,
            thirdLeader: null,
            username: null,
		},
		token: uni.getStorageSync('token') ? uni.getStorageSync('token') : '',
		accessToken: uni.getStorageSync('accessToken') ? uni.getStorageSync('accessToken') : '',
		identifier: uni.getStorageSync('identifier') ? uni.getStorageSync('identifier') : '',
        myInfo: {},
        userProfile: {},
        patientCard: {}, // 患者卡信息
        blacklist: []
	}),
	getters: {
		isLogin: (state) => {
			return state.token !== '' && state.accessToken !== ''
		},
        hasUserInfo: state => state.userInfo && state.userInfo.id,
		getToken(state) {
			return state.token
		},
		getAccessToken(state) {
			return state.accessToken
		},
		getIdentifier(state) {
			return state.identifier
		}
	},
	actions: {
        setPatientCard(data) {
            this.patientCard = data
        },
        async getUserInfo(refresh = false) {
            try {
                if (refresh || !this.hasUserInfo) {
                    const {data} = await getUserInfo();
                    this.userInfo = data;
                    console.log( data)
                }
            } catch (error) {
                console.log(error);
            }
        },
		// 添加手机号登录方法
		async loginByPhone(userInfo) {
            try {
				const {data} = await loginByPhone(userInfo);
                console.log( data)
                if (data.access_token) {
                    this.token = data.access_token;
                    this.accessToken = data.accessToken;
                    uni.setStorageSync('token', data.access_token)
                    uni.setStorageSync('accessToken', data.access_token)
                    uni.$u.toast('登陆成功')
                    uni.navigateBack()
                }
			} catch (error) {
				console.log(error);
			}
		},
		
		// 添加退出登录方法
		logout() {
			// 清除状态
			this.token = '';
			this.accessToken = '';
			this.identifier = '';
			this.userInfo = {
				openid: '',
				session_key: ''
			};
			this.myInfo = {};
			this.userProfile = {};
			
			// 清除本地存储
			uni.removeStorageSync('token');
			uni.removeStorageSync('accessToken');
			uni.removeStorageSync('identifier');
			uni.removeStorageSync('openid');
			uni.removeStorageSync('session_key');
		},
		
		// 添加更新用户资料方法
		updateUserProfile(profile) {
			this.userProfile = profile;
		},
		
		// 添加更新我的信息方法
		updateMyInfo(info) {
			this.myInfo = info;
		},
		
		// 添加更新 token 方法
		newToken(token) {
			this.token = token;
			uni.setStorageSync('token', token);
		},
		
		// 添加更新 accessToken 方法
		newAccessToken(accessToken) {
			this.accessToken = accessToken;
			uni.setStorageSync('accessToken', accessToken);
		},
		
		// 重置用户
		resetUser() {
			this.token = '';
			this.accessToken = '';
			this.identifier = '';
			this.userInfo = {
				openid: '',
				session_key: ''
			};
			this.myInfo = {};
			this.userProfile = {};
		}
	},
});

export default useUserStore;